package com.ewanghuiju.app.component;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;

import com.ewanghuiju.app.app.App;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.ui.web.QBSDKUtils;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;
import com.tencent.smtt.sdk.QbSdk;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.socialize.PlatformConfig;

import cn.jiguang.jmlinksdk.api.JMLinkAPI;

/**
 * @author sj  qq:751943509
 * @class describe
 * @time 2020/2/10
 */
public class InitializeService extends IntentService {

    private static final String ACTION_INIT = "initApplication";

    public InitializeService() {
        super("InitializeService");
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, InitializeService.class);
        intent.setAction(ACTION_INIT);
        context.startService(intent);
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        if (intent != null) {
            final String action = intent.getAction();
            if (ACTION_INIT.equals(action)) {
                initApplication();
            }
        }
    }

    private void initApplication() {
        //初始化日志
        Logger.addLogAdapter(new AndroidLogAdapter());
        //初始化错误收集
        CrashHandler.init(new CrashHandler(getApplicationContext()));
        //初始化腾讯bug收集
//        initBugly();

        //初始化组件化基础库, 统计SDK/推送SDK/分享SDK都必须调用此初始化接口
        UMConfigure.init(this, Constants.UMENT_APPKEY, "Umeng", UMConfigure.DEVICE_TYPE_PHONE,
                "");//669c30a9584623e70e8cd01b0381dcb4
        PlatformConfig.setWeixin(Constants.WX_APPID, Constants.WX_SECRET);
        PlatformConfig.setQQZone(Constants.QQ_APPID, Constants.QQ_SECRET);
        //友盟统计
//        MobclickAgent.setSessionContinueMillis(40*1000);

        //初始化内存泄漏检测
//        LeakCanary.install(App.getInstance());
//
//        //初始化过度绘制检测
//        BlockCanary.install(getApplicationContext(), new AppBlockCanaryContext()).start();

        //初始化极光推送
//        JPushInterface.setDebugMode(true);    // 设置开启日志,发布时请关闭日志
//        JPushInterface.init(App.getInstance());            // 初始化 JPush
        JMLinkAPI.getInstance().init(App.getInstance());   //极光魔链
//        JMLinkAPI.getInstance().setDebugMode(false);  //是否打开debug
        JMLinkAPI.getInstance().registerWithAnnotation();//开启注解绑定
        //  预加载X5内核
        QbSdk.setDownloadWithoutWifi(true);
        QBSDKUtils.init(App.getInstance(), false);
        //初始化tbs x5 webview
//        QbSdk.allowThirdPartyAppDownload(true);
//        QbSdk.initX5Environment(getApplicationContext(), QbSdk.WebviewInitType.FIRSTUSE_AND_PRELOAD, new QbSdk.PreInitCallback() {
//            @Override
//            public void onCoreInitFinished() {
//            }
//
//            @Override
//            public void onViewInitFinished(boolean b) {
//            }
//        });
    }

//    private void initBugly() {
//        Context context = getApplicationContext();
//        String packageName = context.getPackageName();
//        String processName = SystemUtil.getProcessName(android.os.Process.myPid());
//        CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(context);
//        strategy.setUploadProcess(processName == null || processName.equals(packageName));
//        CrashReport.initCrashReport(context, Constants.BUGLY_ID, isDebug, strategy);
//    }
}
